package com.aiit.carinsurance2.controller;

import com.aiit.carinsurance2.pojo.*;
import com.aiit.carinsurance2.service.ToubaodanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

@Controller
public class ToubaodanController {

    @Autowired
    private ToubaodanService toubaodanService;

    @RequestMapping("/toubaodan")
    public String getto(){
        return "toubaodan";
    }
    @GetMapping("/toubaodan")
    public ModelAndView gettoubaodan(){
        ModelAndView mav=new ModelAndView();
        String states="待审核";
        List<Toubaodan> toubaodan=toubaodanService.query(states);
        //mav.addObject("toubaodan",toubaodan);
        System.out.println(toubaodan);
//        mav.addObject("mgg","未收到任何审核");
        if (toubaodan.isEmpty()) {
            System.out.println(222);
            mav.addObject("msg","未收到任何审核");
            mav.setViewName("toubaodan1");
        }
        else {
          List<Toubaodaninfo> toubaodaninfo = toubaodanService.queryinfo(states);
            System.out.println(toubaodaninfo);
            mav.addObject("toubaodaninfo", toubaodaninfo);
            mav.setViewName("toubaodan");
        }
        return mav;
    }

    @GetMapping("/update1")
    public String getupdate(){return "toubaodan";}
    @PostMapping("/update1")
    @ResponseBody
    public Integer update1(String id){
        Toubaodan  toubaodan=  toubaodanService.querybyid(id);
        System.out.println(toubaodan);
        String states="通过";
        String bec="无";
        toubaodan.setStates(states);
        toubaodan.setBec(bec);
        System.out.println(toubaodan);
        int rlt=toubaodanService.update(toubaodan);
        return rlt;
    }


    @GetMapping("/aa")
    public String doaa(){
        return "aa";
    }
    @PostMapping("querytoubaodan")
    @ResponseBody
    public Toubaodaninfo query(String id ){
        int i = Integer.parseInt(id);
        Toubaodaninfo querytoubaodaninfo = toubaodanService.querytoubaodaninfo(i);
        System.out.println(querytoubaodaninfo);
        return querytoubaodaninfo;

    }

    @GetMapping("/insert")
    @ResponseBody
    public Integer getinsert(HttpSession session) {
        Object obj = session.getAttribute("user");
        User u = (User) obj;
        Integer uid = u.getUid();
        Random random = new Random();
        int ran = random.nextInt(10000);
        List<Insured> insured = toubaodanService.querybuid(uid);
        List<Car> car = toubaodanService.querycarid(uid);
        List<Userinsurancepay> allpay = toubaodanService.queryallpay(uid);

        Toubaodan toubaodan = new Toubaodan();
        toubaodan.setUid(uid);
        toubaodan.setBuid(insured.get(0).getBuid());
        toubaodan.setCarid(car.get(0).getCid());
        toubaodan.setAllpay(allpay.get(0).getCost());
        toubaodan.setBec("");
        toubaodan.setStates("待审核");
        toubaodan.setPolicyid(ran);
        int rlt=toubaodanService.inserts(toubaodan);
        System.out.println(rlt);
        return rlt;
    }
    @PostMapping("updatetoubaodaninfo")
    public String postupdate(Toubaodaninfo toubaodaninfo){
        Integer id=toubaodaninfo.getId();
        String states=toubaodaninfo.getStates();
        String bec=toubaodaninfo.getBec();
        int rlt=toubaodanService.updateinfo(states,bec,id);
        return "styles";
    }

    @GetMapping("toubaodanresult")
    public  String getresult(){
        return "toubaodanresult";
    }
    @GetMapping("querytoubaodan")
    public ModelAndView gettoubaodan(HttpSession session){
        Object obj = session.getAttribute("user");
        User u = (User) obj;
        Integer uid = u.getUid();
        ModelAndView mav=new ModelAndView();

        Toubaodaninfo toubaodaninfo=toubaodanService.queryinfobyuid(uid);
        if(toubaodaninfo.getStates().equals("未通过")){
            session.setAttribute("msgs","抱歉，您的审核未通过");
        }
        else if(toubaodaninfo.getStates().equals("通过")){
            session.setAttribute("msgs","恭喜您，您的审核已通过");
        }else {
            session.setAttribute("msgs","您尚未提交审核");
        }
        mav.addObject("toubaodaninfo",toubaodaninfo);
        mav.setViewName("toubaodanresult");
        return mav;
    }


}
